package com.bite.java_chatroom.mapper;

import com.bite.java_chatroom.model.MessageResp;
import com.bite.java_chatroom.model.UserInfo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface FriendMapper {
    //通过用户好友关系表，查好友id
    @Select("select id,username from user_info where id in " +
            "(select friend_id from friend where delete_flag = 1 and user_id = #{id}) and delete_flag = 1;")
    List<UserInfo> selectFriendById(Integer id);

    @Select("select id,username from user_info where username like #{userName} and id != #{myselfId}")
    List<UserInfo> selectLikeUserInfo(String userName,Integer myselfId);

    @Insert("insert into add_friend values (#{fromId}, #{fromName}, #{toId}, #{content}, now(), 1,0,1)")
    void addFriendInvite(Integer fromId,String fromName,Integer toId, String content);

    @Insert("insert into add_friend values (#{fromId}, #{fromName}, #{toId}, #{content}, now(), 1,0,0)")
    void addFriendInviteNoOnline(Integer fromId,String fromName,Integer toId, String content);

    @Update("update add_friend set delete_flag = 0 , result = 2 where from_id = #{fromId} and to_id = #{userId}")
    void removeAddFriend(Integer fromId,Integer userId);

    @Update("update add_friend set delete_flag = 0 , result = 1 where from_id = #{fromId} and to_id = #{userId}")
    void removeAddFriend2(Integer fromId,Integer userId);

    @Insert("insert into friend (user_id,friend_id) values (#{userId1}, #{userId2})")
    void insertFriend(Integer userId1,Integer userId2);

    @Select("select from_id,from_name,content from add_friend where to_id = #{toId} and delete_flag = 1 and result = 0 and online = 0")
    List<MessageResp> selectAddFriends(Integer toId);

    @Select("select count(1) from friend where user_id = #{userId} and friend_id = #{friendId}")
    Integer selectFriendByUserId(Integer userId,Integer friendId);

    @Update("update add_friend set online = 0 where to_id = #{toId}")
    Integer setAddFriendState(Integer toId);
}
